package com.hzcx.app.simplechat.ui.group;

import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.hzcx.app.simplechat.R;
import com.hzcx.app.simplechat.api.BaseObserver;
import com.hzcx.app.simplechat.base.BaseActivity;
import com.hzcx.app.simplechat.base.BaseEmptyBean;
import com.hzcx.app.simplechat.model.GroupModel;
import com.hzcx.app.simplechat.ui.friend.bean.FriendCityBean;
import com.hzcx.app.simplechat.ui.group.LiveData.GroupInfoBeanLiveData;
import com.hzcx.app.simplechat.ui.group.adapter.GroupMuteMemberAdapter;
import com.hzcx.app.simplechat.ui.group.bean.GroupInfoBean;
import com.hzcx.app.simplechat.ui.group.bean.MembersBean;
import com.hzcx.app.simplechat.ui.moment.bean.MomentBean;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

public class GroupJoinApplyActivity extends BaseActivity {

    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.iv_title_right)
    ImageView ivTitleRight;
    @BindView(R.id.tv_title_right)
    TextView tvTitleRight;

    @BindView(R.id.setting_group_join_apply_members_list)
    RecyclerView applyMembersList;


//    public static String GroupInfoBeanKey = "GroupJoinApplyActivityGroupInfoBean";
//    GroupInfoBean groupInfoBean;
    public static String GroupInfoBeanKeyGroupID = "QrCodeActivity_GroupInfoBeanKeyGroupID";
    GroupInfoBean groupInfoBean;
    private String groupID;

    GroupMuteMemberAdapter adapter;
    List<MembersBean> joinList;

    @Override
    public int getLayout() {
        return R.layout.activity_group_join_apply_list;
    }

    @Override
    public void initPresenter() {

    }

    @Override
    public void initView() {
        tvTitle.setText("进群申请");
//        groupInfoBean = (GroupInfoBean)getIntent().getSerializableExtra(GroupInfoBeanKey);
        groupID = getIntent().getStringExtra(GroupInfoBeanKeyGroupID);
        groupInfoBean = GroupInfoBeanLiveData.getGroupInfoBean(groupID);
        joinList = new ArrayList<>();
        adapter = new GroupMuteMemberAdapter(null, joinList);
        adapter.onItemClickListener = new GroupMuteMemberAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(FriendCityBean bean) {
            }

            @Override
            public void onItemClick(MembersBean bean) {
                showLoading();
                GroupModel.examineJoinGroupApply(GroupJoinApplyActivity.this, groupInfoBean.getGroup_id(), String.valueOf(bean.getMember_id()), "2", new BaseObserver<BaseEmptyBean>() {
                    @Override
                    protected void onSuccess(BaseEmptyBean data) {
                        hideLoading();
                        joinList.remove(bean);
                        adapter.notifyDataSetChanged();
                        GroupModel.getGroupInfo(GroupJoinApplyActivity.this, groupInfoBean.getGroup_id(), new BaseObserver<GroupInfoBean>() {
                            @Override
                            protected void onSuccess(GroupInfoBean data) {
                                data.setMembersCount(data.getMembers().size());
                                data.save();
                                GroupInfoBeanLiveData.update(data);
                            }
                        });
                    }
                });
            }
        };
        applyMembersList.setAdapter(adapter);
        applyMembersList.setLayoutManager(new LinearLayoutManager(this));
        adapter.notifyDataSetChanged();
    }

    @Override
    public void initData() {
        GroupModel.joinGroupApplyList(GroupJoinApplyActivity.this, groupInfoBean.getGroup_id(), new BaseObserver<List<MembersBean>>() {

            @Override
            protected void onSuccess(List<MembersBean> data) {
                joinList.clear();
                joinList.addAll(data);
                adapter.notifyDataSetChanged();
            }
        });
    }

}
